﻿Public Class DungeonMap
    Private map(,) As Integer
    Private collisionMesh(,) as Integer
    Private dungeonRooms() As mapGenerator.Room

    Sub New(ByVal sizeX, ByVal sizeY)
        ReDim map(sizeX, sizeY)
        ReDim collisionMesh(sizeX, sizeY)
        mapGenerator.GenerateDungeonMap(map, dungeonRooms, collisionMesh)
    End Sub

    Sub New(ByVal size)
        ReDim map(size, size)
        ReDim collisionMesh(size, size)
        mapGenerator.GenerateDungeonMap(map, dungeonRooms, collisionMesh)
    End Sub

    'Testing Constructor
    Sub New()
        ReDim map(100, 100)
        ReDim collisionMesh(100, 100)
        dungeonRooms(0) = New mapGenerator.Room(30, 30, 10, 10)
        map(11, 11) = 3
    End Sub

    Public Function getTiles(ByVal xCoord, ByVal yCoord, ByVal size) As Integer(,)
        Dim tiles(size, size) As Integer

        For i As Integer = xCoord To xCoord + size
            For j As Integer = yCoord To yCoord + size
                tiles(i, j) = map(i, j)
            Next
        Next
        'TODO: If you select an area that would be outside of map area, draw blanks.
        Return tiles
    End Function

    Public Function getTile(ByVal xCoord, ByVal yCoord) As Integer
        If xCoord > map.GetLength(0) - 1 Or yCoord > map.GetLength(1) - 1 Then
            Return 0
        Else
            If xCoord >= 0 And yCoord >= 0 Then
                Return map(xCoord, yCoord)
            Else
                Return 0
            End If
        End If
    End Function

    Sub changeTile(xCoord As Integer, yCoord As Integer, index As Integer)
        If xCoord < map.GetLength(0) And xCoord >= 0 Then
            If yCoord < map.GetLength(1) And yCoord >= 0 Then
                map(xCoord, yCoord) = index
            End If
        End If
    End Sub

    Function getSizeX() As Integer
        Return map.GetLength(0)
    End Function

    Function getSizeY() As Integer
        Return map.GetLength(1)
    End Function

    Function collides(xPos As Integer, yPos As Integer) As Boolean
        Dim doesCollide As Integer
        If xPos > collisionMesh.GetLength(0) - 1 Or yPos > collisionMesh.GetLength(1) - 1 Then
            doesCollide = 1
        Else
            If xPos >= 0 And yPos >= 0 Then
                doesCollide = collisionMesh(xPos, yPos)
            Else
                doesCollide = 1
            End If
        End If
        If doesCollide <> 0 Then
            Return True
        Else : Return False
        End If
    End Function

    Public Function isInsideDungeon(xCoord As Integer, yCoord As Integer) As Boolean
        If xCoord < 0 Or xCoord > map.GetLength(0) Or yCoord < 0 Or yCoord > map.GetLength(1) Then
            Return False
        Else
            If map(xCoord, yCoord) > 1 Then
                Return True
            Else
                Return False
            End If
        End If
    End Function
End Class
